package com.lqfan.autoAnswerQ.controller.notebook;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lqfan.autoAnswerQ.controller.importQuestions.PoiWordController;
import com.lqfan.autoAnswerQ.model.subject.Subject;
import com.lqfan.autoAnswerQ.service.notebook.INoteBookService;
import com.lqfan.autoAnswerQ.utils.ResponseMessage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 添加笔记
 * @author zmlin
 * @since 2020/4/11
 */
@RestController
@RequestMapping(value = "/note",produces = MediaType.APPLICATION_JSON_VALUE)
@Api(value = "笔记")
public class NotebookController {
    private static final Logger logger = LoggerFactory.getLogger(PoiWordController.class);

    @Autowired
    INoteBookService noteBookService;

    //注册
    @ApiOperation(value = "添加笔记",notes = "添加笔记")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "subjectId",value = "题目Id",required = true,dataType = "String"),
                    @ApiImplicitParam(name = "userId",value = "用户Id",required = true,dataType = "String"),
                    @ApiImplicitParam(name = "noteContent",value = "笔记内容",required = true, dataType = "String")
            }
    )
    @RequestMapping(value = "addNote",method = RequestMethod.POST)
    public ResponseMessage addNote(
            @RequestParam(value = "userId") String userId,
            @RequestParam(value = "subjectId") String subjectId,
            @RequestParam(value = "noteContent") String noteContent){
        logger.info("开始进行添加笔记");
        boolean note =noteBookService.insertNoteBook(userId,subjectId,noteContent);
        if (note){
            return ResponseMessage.success("添加笔记成功");
        }else {
            return ResponseMessage.fail("添加笔记失败");
        }

    }


    @ApiOperation(value = "删除笔记或题目", notes = "删除笔记或题目")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", required = true, dataType = "String"),
            @ApiImplicitParam(name = "id", value = "对应笔记的id", required = true, dataType = "String")
    })
    @RequestMapping(value = "delNote", method = RequestMethod.POST)
    public ResponseMessage delErrorNote(@RequestParam(value = "userId") String userId,
                                          @RequestParam(value = "id") String id,
                                        @RequestParam(value = "subjectId") String subjectId) {
        logger.info("删除笔记或题目");
        int req = checkOneNote(subjectId);
        switch (req){
            case 0:
                logger.info("没有找到改题目");
                return ResponseMessage.fail("没有找到改题目");
            default:
                boolean res;
                if (id.equals("无")){
                    //删除题目
                    QueryWrapper queryWrapper = new QueryWrapper();
                    queryWrapper.eq("userId",userId);
                    queryWrapper.eq("subjectId",subjectId);
                    res = noteBookService.remove(queryWrapper);
                }else {
                    //删除笔记
                    QueryWrapper queryWrapper = new QueryWrapper();
                    queryWrapper.eq("userId",userId);
                    queryWrapper.eq("id",id);
                    res = noteBookService.remove(queryWrapper);
                }

                if(!res) {
                    logger.info("删除对应笔记失败");
                    return ResponseMessage.fail("删除失败");
                }
                logger.info("删除对应笔记成功");
                return ResponseMessage.success("删除成功");

        }
    }

    @ApiOperation(value = "获取对应题目所有笔记", notes = "获取对应题目所有笔记")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", required = true, dataType = "String"),
            @ApiImplicitParam(name = "subjectId", value = "题目id", required = true, dataType = "String")
    })
    @RequestMapping(value = "getSubjectAllNote", method = RequestMethod.POST)
    public ResponseMessage getSubjectAllNote(@RequestParam(value = "userId") String userId,
                                   @RequestParam(value = "subjectId") String subjectId) {
        logger.info("获取对应题目所有笔记");
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("userId",userId);
        queryWrapper.eq("subjectId",subjectId);
        List<String> res = noteBookService.listObjs(queryWrapper);
        if(res.size()==0) {
            logger.info("暂无笔记信息");
            return ResponseMessage.fail("获取失败");
        }
        logger.info("获取笔记成功");
        return ResponseMessage.success(res);
    }

    @ApiOperation(value = "获取题目笔记", notes = "获取题目笔记")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", required = true, dataType = "String")
    })
    @RequestMapping(value = "getSubjectNote", method = RequestMethod.POST)
    public ResponseMessage getSubjectNote(@RequestParam(value = "userId") String userId) {
        logger.info("获取题目");
        List<Subject> res = noteBookService.getNoteSubject(userId);
        if(res.size()==0) {
            logger.info("暂无信息");
            return ResponseMessage.fail("获取失败");
        }
        logger.info("获取成功");
        return ResponseMessage.success(res);
    }


    public int checkOneNote(String subjectId){
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("subjectId",subjectId);
        return noteBookService.count(queryWrapper);
    }


}
